package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.R;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 登录功能
 *      1.判断用户名是否存在
 *      2.将输入的明文密码转为MD5,然后判断密码是否与数据库密码一致
 *      3.判断用户的封禁状态(STATUS)是否为已封禁
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;

    @Override
    public R<Employee> login(Employee employee) {
        Employee dbEmployee = employeeMapper.login(employee);

        if (dbEmployee == null){
            return R.error("用户名不存在");
        }

        //将输入的密码转换为MD5表
        String md5PassWord = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());

        if (!md5PassWord.equals(dbEmployee.getPassword())){
            return R.error("密码错误");
        }

        if (dbEmployee.getStatus()!= 1){
            return R.error("用户已被封禁");
        }

        return R.success(dbEmployee);
    }

    /**
     * 新增员工功能
     *      1.网页上没有输入密码,在方法内添加密码
     *      2.默认账户为启用状态(STATUS=1)
     *      3.将当前时间传入对象
     * @param employee
     */
    @Override
    public void save(Employee employee) {
        String md5PassWord = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(md5PassWord);
        employee.setStatus(1);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        employeeMapper.save(employee);

    }

    /**
     * 根据姓名查询功能
     * @param page  当前页
     * @param pageSize  一页显示的数据长度
     * @param name  员工姓名
     * @return
     */
    @Override
    public Page<Employee> findByName(Integer page, Integer pageSize, String name) {
        //分页插件的方法,设置当前页和显示数据
        PageHelper.startPage(page,pageSize);
        //查找到一个集合
        List<Employee> employeeList = employeeMapper.findByName(name);
        //将集合作为参数传入PageInfo里
        PageInfo<Employee> employeePageInfo = new PageInfo<>(employeeList);

        Page<Employee> pageEmployee = new Page<>(employeePageInfo.getList(),
                                                employeePageInfo.getTotal());

        return pageEmployee;

    }

    /**
     * 修改功能
     * @param employee
     */
    @Override
    public void update(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        employeeMapper.update(employee);
    }

    /**
     * 根据ID查找功能
     * @param id
     * @return
     */
    @Override
    public R<Employee> findById(Long id) {
        Employee employee = employeeMapper.findById(id);
        return R.success(employee);
    }


}
